package cn.edu.swufe.surverpark.struts2.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.edu.swufe.surverpark.model.Right;
import cn.edu.swufe.surverpark.model.Role;
import cn.edu.swufe.surverpark.struts2.service.RightService;
import cn.edu.swufe.surverpark.struts2.service.RoleService;

/**
 * @author 陈龙 E-mail:529914446@qq.com
 * @version创建时间:2015年5月6日下午8:20:38 类说明
 */
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4210456130297762457L;
	@Resource
	private RoleService roleService;
	@Resource
	private RightService rightService;
	private List<Role> allRoles;
	private List<Right> noOwnRights;
	private Integer[] ownRightIds;

	public String findAllRoles() {
		this.allRoles = roleService.findAllEntities();
		return "roleListPage";
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(List<Role> allRoles) {
		this.allRoles = allRoles;
	}

	public String toAddRolePage() {
		this.noOwnRights = rightService.findAllEntities();
		return "AddRolePage";
	}
	
	public String saveOrUpdateRole(){
		
		roleService.saveOrUpdateRole(model,ownRightIds);
		return "findAllRolesAction";
	}
	
	private Integer roleId;
	
	public String editRole(){
		
		this.model = roleService.getEntity(roleId);
		this.noOwnRights = rightService.findRightsNotInRange(model.getRights());
		return "AddRolePage";
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public List<Right> getNoOwnRights() {
		return noOwnRights;
	}

	public void setNoOwnRights(List<Right> noOwnRights) {
		this.noOwnRights = noOwnRights;
	}

	public Integer[] getOwnRightIds() {
		return ownRightIds;
	}

	public void setOwnRightIds(Integer[] ownRightIds) {
		this.ownRightIds = ownRightIds;
	}

	public String deleteRole(){
		Role role = new Role();
		role.setId(roleId);
		roleService.deleteEntity(role);
		return "findAllRolesAction";
	}

	
	

}
